import china from '../../assets/china.json'


let Mapping = {};

for (let i = 0; i < china.length; i++) {
    const element = china[i];
    Mapping[element.cityCode] = element;
    if (element.children.length) {
        for (let y = 0; y < element.children.length; y++) {
            const child = element.children[y];
            Mapping[child.cityCode] = child;
            if (child.children && child.children.length) {
                for (let d = 0; d < child.children.length; d++) {
                    const item = child.children[d];
                    Mapping[item.cityCode] = item;
                }
            }
        }
    }
}
const state = {
    Mapping: Mapping,
    provinceList: china,
    cityList: [],
    districtList: [],
    SelectValue: {
        province: '',
        district: '',
        city: '',
    }
};
const getters = {

};
const mutations = {
    setCityList(state, val) {
        if (state.Mapping[val] && state.Mapping[val].children) {
            state.cityList = state.Mapping[val].children;
        }
        state.SelectValue.city = '';
        state.SelectValue.district = '';
        state.districtList = [];
    },
    setDistrictList(state, val) {
        if (state.Mapping[val] && state.Mapping[val].children) {
            state.districtList = state.Mapping[val].children;
        }
        state.SelectValue.district = '';
    },
};
const actions = {
    getValue(state) {
        return state.SelectValue
    }
};
export default {
    namespaced: true,
    state,
    getters,
    mutations,
    actions
}